package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public OverviewVO getOverview() {
        //1.查询redis缓存中的数据
        log.info("从redis中，获取线索和商机的概览数据");
        Object object = redisTemplate.opsForValue().get("overviewData");

        if (object != null){//缓存中有数据
            log.info("从redis中，获取线索和商机的概览数据,并返回");
            return (OverviewVO) object;
        }

        //2.缓存中没有数据，再去数据库中查询
        //2.1. 获取线索对应的数据
        OverviewVO clueOverviewData = clueMapper.getClueOverviewData();

        //2.2. 获取商机对应数据
        OverviewVO businessOverviewData = businessMapper.getBusinessOverviewData();

        //2.3.封装OverviewVO，合并数据并返回
        BeanUtils.copyProperties(clueOverviewData,businessOverviewData,"businessTotal",
                "businessWaitAllot","businessWaitFollow","businessFollowing","businessFalse","businessConvertCustomer");

        //3.将数据缓存到redis中,并设置过期时间
        log.info("将数据缓存到redis中");
        redisTemplate.opsForValue().set("overviewData",businessOverviewData,2, TimeUnit.MINUTES);
        return businessOverviewData;
    }
}
